<?php
/*
 * @Date: 2021-01-28 22:57:40
 * @名称: 漫画 · 操作控制器 · 保存漫画章节
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-01-29 00:42:31
 * @FilePath: /Site/app/useradmin/controller/Comics/Operation/ComicsSaveProject.php
 */

namespace app\useradmin\controller\Comics\Operation;

use app\BaseController;
use think\facade\Db;

class ComicsSaveProject extends BaseController
{
    /**
     * @方法名: 保存漫画分卷信息
     * @作者: 初雪桜
     * @信息: 主要用来保存新上传的图片
     * @param String $projectName 分卷名称
     * @param String $subsection 分卷编号
     * @param String $time 时间戳
     * @param String $encryption_bookId 书籍id 加密后
     * @param String $encryption_projectId 章节id 加密后
     * @param String $bookId 书籍id 加密前
     * @param String $projectId 章节id 加密前
     * @param Json $imageList 图片列表
     * @param String $hashHmac_bookId 加密后
     * @param String $hashHmac_projectId 加密后
     * @return Json
     */
    public function SaveProject($projectName, $subsection, $time, $encryption_bookId, $encryption_projectId, $bookId, $projectId, $imageList, $hashHmac_bookId, $hashHmac_projectId)
    {
        //记数类
        $removeNameCount = 0; //统计改名字的文件
        $deleteCount = 0; //统计改名字的文件
        $errorCount = 0; //出现错误统计
        $isOrder = true; //出现错误统计
        if ( //判断加密验证是否一样
            $projectName &&
            $subsection &&
            $time &&
            $encryption_bookId &&
            $encryption_projectId &&
            $bookId &&
            $projectId &&
            $imageList &&
            $encryption_bookId ==
            $hashHmac_bookId &&
            $encryption_projectId ==
            $hashHmac_projectId
        ) {
            if ($time + 3600 > time()) {
                $project_info =  Db::table('book_info_project')
                    ->where([
                        "_id" => $bookId,
                        "chapter_id" => $projectId,
                    ])
                    ->field([
                        "database_table_name", //图片所在数据库
                        "order", //查询分卷信息
                        "project_title", //查询标题
                    ])
                    ->select();

                if (count($project_info) > 1) {
                    return errorJsonReturn("Comics-10000");
                }
                if (count($project_info) < 1) {
                    return errorJsonReturn("Comics-10001");
                }
                //!判断 是否修改了基本信息
                if ($project_info[0]['order'] != $subsection) {
                    //#更新分卷序列
                    $order =  Db::table('book_info_project')
                        ->where([
                            "_id" => $bookId,
                            "order" => $subsection,
                        ])
                        ->find();
                    if ($order) {
                        $isOrder = false;
                    } else {
                        Db::table('book_info_project')
                            ->where([
                                "_id" => $bookId,
                                "chapter_id" => $projectId,
                            ])
                            ->update([
                                "order" => $subsection,
                            ]);
                        $isOrder = true;
                    }
                }

                if ($project_info[0]['project_title'] != $projectName) {
                    Db::table('book_info_project')
                        ->where([
                            "_id" => $bookId,
                            "chapter_id" => $projectId,
                        ])
                        ->update([
                            "project_title" => $projectName,
                        ]);
                }

                //!更新 漫画信息
                $database_name = $project_info[0]['database_table_name'];
                $tempComicsProjectList = Db::table($database_name) //查询全部图片 当前章节的
                    ->where([
                        "_id" => $bookId,
                        "project_id" => $projectId,
                    ])
                    ->select();
                $mysqlDataList = [];
                foreach ($tempComicsProjectList as $key => $value) {
                    unset($value['id']);
                    $mysqlDataList[$value['frame_id']]  = $value;
                }
                Db::table($database_name) //查询全部图片 当前章节的
                    ->where([
                        "_id" => $bookId,
                        "project_id" => $projectId,
                    ])
                    ->delete();
                foreach ($imageList as $key => $value) { //更新数据
                    if (isset($mysqlDataList[$value['imagesId']])) {
                        if ($mysqlDataList[$value['imagesId']]['original_name'] != $value['filename']) {
                            $mysqlDataList[$value['imagesId']]['original_name'] = $value['filename'];
                            $removeNameCount++;
                        }
                        Db::table($database_name)
                            ->insert($mysqlDataList[$value['imagesId']]);
                        unset($mysqlDataList[$value['imagesId']]);
                    } else {
                        $errorCount++; //出现错误统计
                    }
                }
                $deleteCount = count($mysqlDataList);



                return successJsonReturn([
                    "removeNameCount" => $removeNameCount,
                    "deleteCount" => $deleteCount,
                    "errorCount" => $errorCount,
                    "isOrder" => $isOrder,
                ]);
            } else {
                return errorJsonReturn("Other-10000"); //超时
            }
        } else {
            return errorJsonReturn("Comics-10002"); //超时
        }
    }
}
